package com.loren.demo21.runner;

import com.loren.demo21.task.TaskRegister;
import com.loren.demo21.task.TaskRunnable;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

@Component
@AllArgsConstructor
public class TaskRunner implements CommandLineRunner {

    private final TaskRegister taskRegister;

    @Override
    public void run(String... args) {
        List<TaskRunner.Task> taskList = Arrays.asList(
                TaskRunner.Task.builder().bean("taskService").method("task01").expression("0/10 * * * * ?").build(),
                TaskRunner.Task.builder().bean("taskService").method("task02").expression("0/15 * * * * ?").build()
        );
        for (TaskRunner.Task task : taskList) {
            taskRegister.addCronTask(new TaskRunnable(task.getBean(), task.getMethod()), task.getExpression());
        }
    }

    @Getter
    @Setter
    @Builder
    private static class Task {

        private String bean;

        private String method;

        private String expression;
    }
}